package com.usc.csci588.chaltrojan.location.map;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

/**
 * https://developers.google.com/maps/documentation/android/hello-mapview
 * 
 * This is to add overlays and markers at the top of the MapView that is visible to us
 * 
 * @author jaspreet
 *
 */

public class CTItemizedOverlay extends ItemizedOverlay<OverlayItem>  {

	protected ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	protected Context mContext;
	protected boolean isPinch  =  false;

	
	public CTItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		
	}
	
	public CTItemizedOverlay(Drawable defaultMarker, Context context) {
		  super(boundCenterBottom(defaultMarker));
		  mContext = context;
		}

	public void addOverlay(OverlayItem overlay) {
	    mOverlays.add(overlay);
	    populate();
	}
	
	/**
	 * When the populate() method executes, it will call createItem(int) 
	 * in the ItemizedOverlay to retrieve each OverlayItem. 
	 * You must override this method to properly read from the ArrayList
	 *  and return the OverlayItem from the position specified by the given integer.
	 */
	@Override
	protected OverlayItem createItem(int i) {
		  return mOverlays.get(i);
	}

	@Override
	public int size() {
		 return mOverlays.size();
	}
	
	/**
	 * the onTap() callback method, which will handle the event when an item is tapped by the user
	 */
	@Override
	protected boolean onTap(int index) {

		Log.d("maps", "I am insisde onTap function -- index = "+index);
		
		/**
		 * Figure out a way to use this to select the location
		 * that is destination and display the route 
		 */
		/*
		OverlayItem item = mOverlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		
		
		
		
		Toast.makeText(mContext, "You tapped "+mOverlays.get(index).toString(), Toast.LENGTH_LONG).show();
		*/
		
		/**
		 * This is default one 
		 */
		///*
		OverlayItem item = mOverlays.get(index);
		try {
			AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
			dialog.setTitle(item.getTitle());
			dialog.setMessage(item.getSnippet());
			dialog.show();
		} catch (Exception e){
			Toast.makeText(mContext, " "+item.getSnippet().toString(), Toast.LENGTH_LONG).show();
		}
		//*/
		
		
		/*

		GeoPoint startPoint = mOverlays.get(0).getPoint();
		
		StringBuilder urlString = new StringBuilder("");
		urlString.append("http://maps.google.com/maps?saddr=");
    	urlString.append(Double.toString(startPoint.getLatitudeE6()/ (double)1E6));
    	urlString.append(",");
    	urlString.append(Double.toString(startPoint.getLongitudeE6()/ (double)1E6));
    	urlString.append("&daddr=");// to
    	urlString.append(Double.toString(p.getLatitudeE6()/ (double)1E6));
    	urlString.append(",");
    	urlString.append(Double.toString(p.getLongitudeE6()/ (double)1E6));
		
    	final String urlstr = urlString.toString();
		
        AlertDialog.Builder adb = new AlertDialog.Builder(mContext);
        adb.setTitle("Chal, Trojan!");
        	adb.setMessage("You will be redirected outside the app to view navigation instructions..");
        adb.setPositiveButton("OK",
        	new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
					//We have to send this to intent
					try {
						
						Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(urlstr));
						mContext.startActivity(intent);			
			        } catch (ActivityNotFoundException anfe) {
			    		Toast.makeText(mContext, "Your phone has no app for navigation..", Toast.LENGTH_LONG).show();			        	
			        }
				}

        	}
        );
        adb.setNegativeButton("Cancel", null);
        adb.show();
      
	    //*/

		
		return true;
	}
	

//	@Override
//	public boolean onTap(GeoPoint p, MapView map){
//		if (isPinch){
//			return false;
//		}
//		
//		Log.d("maps", "I am insisde onTap function with point p and map m.. Started from : "+mOverlays.get(0).getPoint().toString()+ " TRYING TO GO TO "+p.toString());
//
//			
//		/*
//
//		GeoPoint startPoint = mOverlays.get(0).getPoint();
//		
//		StringBuilder urlString = new StringBuilder("");
//		urlString.append("http://maps.google.com/maps?saddr=");
//    	urlString.append(Double.toString(startPoint.getLatitudeE6()/ (double)1E6));
//    	urlString.append(",");
//    	urlString.append(Double.toString(startPoint.getLongitudeE6()/ (double)1E6));
//    	urlString.append("&daddr=");// to
//    	urlString.append(Double.toString(p.getLatitudeE6()/ (double)1E6));
//    	urlString.append(",");
//    	urlString.append(Double.toString(p.getLongitudeE6()/ (double)1E6));
//		
//    	final String urlstr = urlString.toString();
//		
//        AlertDialog.Builder adb = new AlertDialog.Builder(mContext);
//        adb.setTitle("Chal, Trojan!");
//        	adb.setMessage("You will be redirected outside the app to view navigation instructions..");
//        adb.setPositiveButton("OK",
//        	new DialogInterface.OnClickListener(){
//				@Override
//				public void onClick(DialogInterface dialog, int which) {
//					//We have to send this to intent
//					try {
//						
//						Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(urlstr));
//						mContext.startActivity(intent);			
//			        } catch (ActivityNotFoundException anfe) {
//			    		Toast.makeText(mContext, "Your phone has no app for navigation..", Toast.LENGTH_LONG).show();			        	
//			        }
//				}
//
//        	}
//        );
//        adb.setNegativeButton("Cancel", null);
//        adb.show();
//      
//	    //*/
//		return false;
//	}
//	
//	@Override
//	public boolean onTouchEvent(MotionEvent e, MapView mapView)
//	{
//	    int fingers = e.getPointerCount(); 
//	    if( e.getAction()==MotionEvent.ACTION_DOWN ){
//	        isPinch=false;  // Touch DOWN, don't know if it's a pinch yet
//	    }
//	    if( e.getAction()==MotionEvent.ACTION_MOVE && fingers==2 ){
//	        isPinch=true;   // Two fingers, def a pinch
//	    }
//	    return super.onTouchEvent(e,mapView);
//	}

//	@Override
//	public boolean onDoubleTap(MotionEvent e) {
//		// TODO Auto-generated method stub
//		return false;
//	}
//
//	@Override
//	public boolean onDoubleTapEvent(MotionEvent e) {
//		// TODO Auto-generated method stub
//		return false;
//	}
//
//	@Override
//	public boolean onSingleTapConfirmed(MotionEvent e) {
//		// TODO Auto-generated method stub
//		return false;
//	}
//
//	@Override
//	public boolean onDown(MotionEvent e) {
//		// TODO Auto-generated method stub
//		return false;
//	}
//
//	@Override
//	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
//			float velocityY) {
//		// TODO Auto-generated method stub
//		return false;
//	}
//
//	@Override
//	public void onLongPress(MotionEvent e) {
//		// TODO Auto-generated method stub
//		
//	}
//
//	@Override
//	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
//			float distanceY) {
//		// TODO Auto-generated method stub
//		return false;
//	}
//
//	@Override
//	public void onShowPress(MotionEvent e) {
//		// TODO Auto-generated method stub
//		
//	}
//
//	@Override
//	public boolean onSingleTapUp(MotionEvent e) {
//		// TODO Auto-generated method stub
//		return false;
//	}
//
	
	
}
